//
//  HEMyServiceViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/9.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMyServiceViewController.h"
#import "HEServicePackModel.h"
#import "HEMyServiceCell.h"
#import "HEServicePackDetailsViewController.h"

@interface HEMyServiceViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEServicePackModel *> * dataSourceArr;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;

@end

@implementation HEMyServiceViewController

- (NSMutableArray<HEServicePackModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的服务包";
    
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self.view addSubview:self.tableView];
    [self loadData];
}


- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            weakSelf.isFromHeadRefresh = YES;
            [weakSelf loadData];
        }];

        [_tableView registerClass:[HEMyServiceCell class] forCellReuseIdentifier: KHEMyServiceCell];

    }
    return _tableView;
}


#pragma mark -

- (void)loadData {

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithInteger: HEChannelTypeServicePack] forKey:@"channel"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KMyServer parameters:params success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];

        [self.dataSourceArr addObjectsFromArray:[HEServicePackModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.count == 0) {
                [Dialog toastBottom:@"没有更多数据了~"];
            }
        }

    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}




#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEMyServiceCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHEMyServiceCell];
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHEMyServiceCell cacheByIndexPath:indexPath configuration:^(HEMyServiceCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEServicePackModel *model = self.dataSourceArr[indexPath.row];
 
    HEServicePackDetailsViewController *vc = [HEServicePackDetailsViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

@end

